package mainProcess;

import mainProcess.MainProcessing;
import TraitementMessages.*;
import java.io.Serializable;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.*;
import com.lo23.data.objinterface.*;
import com.lo23.network.IProtocolListener;

/**
 * @class ProtocolListener
 * @author Vincent PALANCHER
 */
public class ProtocolListener implements IProtocolListener {

    private MainProcessing mainProcessing;

    public ProtocolListener (MainProcessing mainProcessing) {
        this.mainProcessing = mainProcessing;
    }

    public void clientDiscovered (String UID, boolean isResponse) {
        if (!isResponse)
        {
            LocalUser lUser = mainProcessing.getDataInterface().getLocalUser(null);
            LoginChallengeMessage loginMessage = new LoginChallengeMessage(lUser.getuID(), lUser.getLogin());
            ArrayList<String> destUIDs = new ArrayList<String>();
            destUIDs.add(UID);
            try {
                this.mainProcessing.getNetworkManager().sendMessage(destUIDs, loginMessage);
            } catch (Exception ex) {
                Logger.getLogger(LoginChallengeMessage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       System.out.println("clientDiscovered");
    }

    public void messageReceived (String UID, Serializable message) {
        if (!(message instanceof BaseMessage))
            return;
        ((BaseMessage)message).handleReceiption(mainProcessing);
        System.out.println("messageReceived");
    }

    public void badMessageReceived (String UID, String error) {
        System.err.println(error);
    }
}
